<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LINE 1 &quot;libraries\\haskeline\\System\\Console\\Haskeline\\Directory.hsc&quot; #-}</span><span id="local-6989586621679091033"></span><span>
</span><span id="line-1"></span><span class="hs-comment">{- |
A Unicode-aware module for interacting with files.  We just need enough to support
filename completion.  In particular, these functions will silently handle all errors
(for example, file does not exist)
-}</span><span>
</span><span id="line-6"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">System.Console.Haskeline.Directory</span><span class="hs-special">(</span><span>
</span><span id="line-7"></span><span>                    </span><span class="hs-identifier">getDirectoryContents</span><span class="hs-special">,</span><span>
</span><span id="line-8"></span><span>                    </span><span class="hs-identifier">doesDirectoryExist</span><span class="hs-special">,</span><span>
</span><span id="line-9"></span><span>                    </span><span class="hs-identifier">getHomeDirectory</span><span>
</span><span id="line-10"></span><span>                    </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-11"></span><span>
</span><span id="line-12"></span><span>
</span><span id="line-13"></span><span class="hs-pragma">{-# LINE 13 &quot;libraries\\haskeline\\System\\Console\\Haskeline\\Directory.hsc&quot; #-}</span><span>
</span><span id="line-13"></span><span>
</span><span id="line-14"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Foreign</span><span>
</span><span id="line-15"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Foreign.C</span><span>
</span><span id="line-16"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">System.Win32.Types</span><span>
</span><span id="line-17"></span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">qualified</span><span> </span><span class="hs-identifier">System.Directory</span><span class="hs-cpp">



#include &quot;windows_cconv.h&quot;
</span><span>
</span><span id="line-23"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-string">&quot;FindFirstFileW&quot;</span><span> </span><span class="hs-identifier">c_FindFirstFile</span><span>
</span><span id="line-24"></span><span>            </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">LPCTSTR</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">HANDLE</span><span>
</span><span id="line-25"></span><span>
</span><span id="line-26"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-string">&quot;FindNextFileW&quot;</span><span> </span><span class="hs-identifier">c_FindNextFile</span><span>
</span><span id="line-27"></span><span>            </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-28"></span><span>
</span><span id="line-29"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-string">&quot;FindClose&quot;</span><span> </span><span class="hs-identifier">c_FindClose</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-30"></span><span>
</span><span id="line-31"></span><span class="hs-identifier">getDirectoryContents</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">FilePath</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">[</span><span class="hs-identifier">FilePath</span><span class="hs-special">]</span><span>
</span><span id="line-32"></span><span class="hs-identifier">getDirectoryContents</span><span> </span><span class="hs-identifier">fp</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">allocaBytes</span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-number">320</span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">findP</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-33"></span><span class="hs-pragma">{-# LINE 33 &quot;libraries\\haskeline\\System\\Console\\Haskeline\\Directory.hsc&quot; #-}</span><span>
</span><span id="line-33"></span><span>    </span><span class="hs-identifier">withCWString</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fp</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-string">&quot;\\*&quot;</span><span class="hs-special">)</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">t_arr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-34"></span><span>        </span><span class="hs-identifier">h</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_FindFirstFile</span><span> </span><span class="hs-identifier">t_arr</span><span> </span><span class="hs-identifier">findP</span><span>
</span><span id="line-35"></span><span>        </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">iNVALID_HANDLE_VALUE</span><span>
</span><span id="line-36"></span><span>            </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">[</span><span class="hs-special">]</span><span>
</span><span id="line-37"></span><span>            </span><span class="hs-keyword">else</span><span> </span><span class="hs-identifier">loop</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-identifier">findP</span><span>
</span><span id="line-38"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-39"></span><span>    </span><span class="hs-identifier">loop</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-identifier">findP</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-40"></span><span>        </span><span class="hs-identifier">f</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">peekFileName</span><span> </span><span class="hs-identifier">findP</span><span>
</span><span id="line-41"></span><span>        </span><span class="hs-identifier">isNext</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_FindNextFile</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-identifier">findP</span><span>
</span><span id="line-42"></span><span>        </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">isNext</span><span>
</span><span id="line-43"></span><span>            </span><span class="hs-keyword">then</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-special">{</span><span class="hs-identifier">fs</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">loop</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-identifier">findP</span><span class="hs-special">;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">f</span><span class="hs-glyph">:</span><span class="hs-identifier">fs</span><span class="hs-special">)</span><span class="hs-special">}</span><span>
</span><span id="line-44"></span><span>            </span><span class="hs-keyword">else</span><span> </span><span class="hs-identifier">c_FindClose</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-operator">&gt;&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">[</span><span class="hs-identifier">f</span><span class="hs-special">]</span><span>
</span><span id="line-45"></span><span>    </span><span class="hs-identifier">peekFileName</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">peekCWString</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">plusPtr</span><span class="hs-special">`</span><span> </span><span class="hs-number">44</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-46"></span><span class="hs-pragma">{-# LINE 46 &quot;libraries\\haskeline\\System\\Console\\Haskeline\\Directory.hsc&quot; #-}</span><span>
</span><span id="line-46"></span><span>
</span><span id="line-47"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-string">&quot;GetFileAttributesW&quot;</span><span> </span><span class="hs-identifier">c_GetFileAttributes</span><span>
</span><span id="line-48"></span><span>            </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">LPCTSTR</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">DWORD</span><span>
</span><span id="line-49"></span><span>
</span><span id="line-50"></span><span class="hs-identifier">doesDirectoryExist</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">FilePath</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-51"></span><span class="hs-identifier">doesDirectoryExist</span><span> </span><span class="hs-identifier">file</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-52"></span><span>    </span><span class="hs-identifier">attrs</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">withCWString</span><span> </span><span class="hs-identifier">file</span><span> </span><span class="hs-identifier">c_GetFileAttributes</span><span>
</span><span id="line-53"></span><span>    </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">attrs</span><span> </span><span class="hs-operator">/=</span><span> </span><span class="hs-special">(</span><span class="hs-number">4294967295</span><span class="hs-special">)</span><span>
</span><span id="line-54"></span><span class="hs-pragma">{-# LINE 54 &quot;libraries\\haskeline\\System\\Console\\Haskeline\\Directory.hsc&quot; #-}</span><span>
</span><span id="line-54"></span><span>            </span><span class="hs-operator">&amp;&amp;</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">attrs</span><span> </span><span class="hs-operator">.&amp;.</span><span> </span><span class="hs-special">(</span><span class="hs-number">16</span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-operator">/=</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-55"></span><span class="hs-pragma">{-# LINE 55 &quot;libraries\\haskeline\\System\\Console\\Haskeline\\Directory.hsc&quot; #-}</span><span>
</span><span id="line-55"></span><span>
</span><span id="line-56"></span><span class="hs-identifier">getHomeDirectory</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">FilePath</span><span>
</span><span id="line-57"></span><span class="hs-identifier">getHomeDirectory</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">System.Directory.getHomeDirectory</span><span>
</span><span id="line-58"></span><span>
</span><span id="line-59"></span><span>
</span><span id="line-60"></span><span class="hs-pragma">{-# LINE 64 &quot;libraries\\haskeline\\System\\Console\\Haskeline\\Directory.hsc&quot; #-}</span><span>
</span><span id="line-64"></span></pre></body></html>